import { getToken, setToken, removeToken } from '@/utils/auth'
import { getUserInfo, getInfo } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || '', // 存入的token值
    userInfo: {} // 用户的信息
  },
  mutations: {
    // 将获取的token存入vuex
    updateToken(state, token) {
      state.token = token
      setToken(token)
    },
    // 获取用户的信息
    getUserInfo(state, info) {
      state.userInfo = info
    },
    // 删除token
    removeToken(state) {
      state.token = '' // 删除vuex的token
      removeToken()
    },
    // 删除用户的信息
    removeUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async getUserInfo(context) {
      try {
        const res = await getUserInfo()
        // console.log(res)
        const info = await getInfo(res.data.userId)
        context.commit('getUserInfo', { ...res.data, ...info.data })
        // console.log(info)
        return res.data.roles.menus
      } catch (error) {
        console.log(error)
      }
    }
  }
}
